找传奇、传世资源到传世资源站!

圆角widget实现方法(roundedwidgets.cpp)

8.5玩家评分(1人评分)
下载后可评
介绍 评论 失效链接反馈

用qt很简单地实现圆角widget,并且不需要使用qss
#include "roundedwidgets.h"#include <QDebug>DGUI_USE_NAMESPACERoundedWidget::RoundedWidget(QWidget *parent, int r, int t, QPoint o): QWidget(parent), radius(r), margin(0), type(t), offset(o), painter(nullptr) {setPalette(DGuiApplicationHelper::instance()->applicationPalette());connect(DGuiApplicationHelper::instance(), &DGuiApplicationHelper::themeTypeChanged, [this]() {setPalette(DGuiApplicationHelper::instance()->applicationPalette());repaint();});if (!offset.isNull())connect(this, &RoundedWidget::widgetMoving, [=](QPoint pos) {this->blockSignals(true);this->move(pos this->offset);this->blockSignals(false);});}void RoundedWidget::paintEvent(QPaintEvent *event) {if (!painter) {painter = new QPainter(this);painter->setRenderHints(QPainter::Antialiasing | QPainter::SmoothPixmapTransform);}painter->begin(this);painter->setRenderHints(QPainter::Antialiasing | QPainter::SmoothPixmapTransform);QPainterPath path;auto rect = event->rect();if (margin)rect.adjust(margin, margin, -margin, -margin);path.addRoundedRect(QRectF(rect), radius, radius);if (type) {auto tmpRect = rect;switch (type) {case TopTwo:tmpRect.adjust(0, rect.height() - radius, 0, 0);break;case BottomTwo:tmpRect.adjust(0, 0, 0, -(rect.height() - radius));break;case LeftTwo:tmpRect.adjust(0, 0, -(rect.width() - radius), 0);break;case RightTwo:tmpRect.adjust(rect.width() - radius, 0, 0, 0);break;}path.addRect(QRectF(tmpRect));}path.setFillRule(Qt::WindingFill);painter->fillPath(path, palette().window());painter->end();}void RoundedWidget::moveEvent(QMoveEvent *event) {emit widgetMoving(event->pos());return QWidget::moveEvent(event);}void RoundedWidget::setRadius(int r) {radius = r;}void RoundedWidget::setMargin(int d) // set self margins{margin = d;setContentsMargins(margin, margin, margin, margin);}void RoundedWidget::setShadow(int d) {if (!d)return;QGraphicsDropShadowEffect *effect = new QGraphicsDropShadowEffect(this);effect->setOffset(0, 0);effect->setColor(QColor(0, 0, 0, 50));effect->setBlurRadius(d);setGraphicsEffect(effect);setMargin(d);}CircleButton::CircleButton(QIcon icon, QWidget *parent) : QPushButton(icon, "", parent), painter(nullptr) {setPalette(DGuiApplicationHelper::instance()->applicationPalette());connect(DGuiApplicationHelper::instance(), &DGuiApplicationHelper::themeTypeChanged, [this]() {setPalette(DGuiApplicationHelper::instance()->applicationPalette());repaint();});}void CircleButton::paintEvent(QPaintEvent *event) {if (!painter) {painter = new QPainter(this);painter->setRenderHints(QPainter::Antialiasing | QPainter::SmoothPixmapTransform);}//qDebug() << "renderHints:" << (painter->renderHints() == (QPainter::Antialiasing | QPainter::SmoothPixmapTransform));//painter->save();painter->begin(this);painter->setRenderHints(QPainter::Antialiasing | QPainter::SmoothPixmapTransform);QPainterPath path;path.addEllipse(event->rect());painter->fillPath(path, isChecked() ? palette().highlight() : palette().button());auto iconRect = event->rect();int d = int(iconRect.width() * 0.3);iconRect.adjust(d, d, -d, -d);painter->drawPixmap(iconRect, this->icon().pixmap(iconRect.width(), iconRect.height()));painter->end();}

评论

发表评论必须先登陆, 您可以 登陆 或者 注册新账号 !


在线咨询: 问题反馈
客服QQ:174666394

有问题请留言,看到后及时答复